import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:wanchong/api/user.dart';
import 'package:wanchong/bean/login_entity.dart';
import 'package:wanchong/bean/success.dart';
import 'package:wanchong/intent/login.dart';
import 'package:wanchong/path.dart';
import 'package:wanchong/utils/common.dart';
import 'package:wanchong/utils/http.dart';
import 'package:wanchong/widget/app_bar.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wanchong/bean/login_go_entity.dart';
class Plogin extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState(); 
}

class _LoginState extends State<Plogin> {
  bool submitA = false;
  bool submitB = false;
  bool passwordShow = true;
  bool deleteShow = false;
  //手机号的控制器
  TextEditingController phoneController = TextEditingController();

  //密码的控制器
  TextEditingController passController = TextEditingController();

  Widget _other(icon, call) {
    return GestureDetector(
      onTap: call,
      child: Container(
        width: 50.0,
        height: 50.0,
        child: Image.asset(icon),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: MyAppBar(
        color: Colors.transparent,
        child: Align(
          alignment: Alignment.centerRight,
          child: GestureDetector(
            onTap: () {
              Navigator.of(context).pushNamed(LoginPath);
            },
            child: Container(
              padding: EdgeInsets.only(right: 20.0),
              child: Text(
                "免密登录",
                style: TextStyle(color: Color(0xFF3C4450), fontSize: 15.0),
              ),
              // color: Colors.red,
            ),
          ),
        ),
      ),
      body: Container(
        margin: EdgeInsets.only(left: 46.0, right: 46.0),
        child: Column(
          children: <Widget>[
            Container(
              width: double.infinity,
              // color: Colors.red,
              margin: EdgeInsets.only(top: 48.0),
              child: Text(
                "免密登录",
                style: TextStyle(
                    fontSize: 28.0,
                    color: Color(0xFF3C4450),
                    fontWeight: FontWeight.w700),
              ),
            ),
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(top: 10.0),
              child: Text(
                "您好，欢迎来到萌宠管家",
                style: TextStyle(
                  fontSize: 18.0,
                  color: Color(0xFF647280),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(
                top: 46.0,
              ),
              decoration: BoxDecoration(),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      maxLength: 11,
                      style:
                          TextStyle(color: Color(0xFF424D59), fontSize: 24.0),
                      keyboardType: TextInputType.phone,
                      controller: phoneController,
                      decoration: InputDecoration(
                          contentPadding:
                              const EdgeInsets.symmetric(vertical: 4.0),
                          border: InputBorder.none,
                          hintText: "请输入手机号",
                          counterText: "",
                          hintStyle: TextStyle(
                              color: Color(0xFFDBDBDB), fontSize: 18.0)),
                      onChanged: (text) {
                        //内容改变的回调
                        if (text.length == 11) {
                          setState(() {
                            this.submitA = true;
                          });
                        } else {
                          setState(() {
                            this.submitA = false;
                          });
                        }
                      },
                    ),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 16.0,
            ),
            Divider(
              color: Color(0xFFDDDDDD),
              height: 1.0,
            ),
            Container(
              margin: EdgeInsets.only(
                top: 10.0,
              ),
              decoration: BoxDecoration(),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      maxLength: 16,
                      obscureText: passwordShow ? true : false,
                      style:
                          TextStyle(color: Color(0xFF424D59), fontSize: 24.0),
                      keyboardType: TextInputType.text,
                      controller: passController,
                      decoration: InputDecoration(
                        counterText: "", //不显示限制长度的字
                        contentPadding:
                            const EdgeInsets.symmetric(vertical: 4.0),
                        border: InputBorder.none,
                        hintText: "请输入密码",
                        hintStyle:
                            TextStyle(color: Color(0xFFDBDBDB), fontSize: 18.0),
                      ),
                      onChanged: (text) {
                        //内容改变的回调
                        if (text.length > 0) {
                          setState(() {
                            deleteShow = true;
                          });
                        }
                        if (text.length < 1) {
                          setState(() {
                            deleteShow = false;
                          });
                        }
                        if (text.length > 7) {
                          setState(() {
                            this.submitB = true;
                          });
                          //print('change $text');
                        }
                        if (text.length < 8) {
                          setState(() {
                            this.submitB = false;
                          });
                        }
                      },
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      onTextClear();
                    },
                    child: Container(
                      margin: EdgeInsets.only(right: 14.0, top: 10.0),
                      width: 20.0,
                      child: deleteShow
                          ? Image.asset(
                              "assets/login/login_input_icon_delete.png")
                          : Container(),
                    ),
                  ),
                  GestureDetector(
                    onTap: () { 
                      eyeOpen();
                    },
                    child: Container(
                      margin: EdgeInsets.only(right: 14.0, top: 10.0),
                      width: 20.0,
                      child: Image.asset(
                        passwordShow
                            ? "assets/login/login_input_icon_eye_a.png"
                            : "assets/login/login_input_icon_eye_b.png",
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(
              height: 16.0,
            ),
            Divider(
              color: Color(0xFFDDDDDD),
              height: 1.0,
            ),
            SizedBox(
              height: 20.0,
            ),
            GestureDetector(
              onTap: () { 
                if (submitA && submitB) {
                  RegExp exp = RegExp(
                      r'^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$');
                  bool matched = exp.hasMatch(phoneController.text);
                  if (!matched) {
                    alert('手机号码格式不正确,请重新输入');
                    return;
                  }
                  pswLogin(phoneController.text, passController.text)
                      .listen((LoginEntity data) {
                        print("=========================");
                    if (data.code == 200) {
                      alert('登录成功');
                      Future.delayed(Duration(seconds: 1), () {
                        Navigator.of(context).pushNamed(HomePath);
                      });
                    }
                  }).onError((err) {
                    MyError e = err;
                    alert(e.message);
                  });
                }
              },
              child: ClipRRect(
                borderRadius: BorderRadius.circular(25.0),
                child: Container(
                  width: double.infinity,
                  height: 50.0,
                  color: submitA==true && submitB==true
                      ? Color(0xFFFFCC33)
                      : Color(0xFFFFE599),
                  child: Center(
                    child: Text(
                      "确认",
                      style:
                          TextStyle(color: Color(0xFF313230), fontSize: 18.0),
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 10.0,
            ),
            GestureDetector(
              onTap: () {
                Navigator.of(context).pushNamed(LoginPath);
              },
              child: Text(
                "忘记密码",
                style: TextStyle(
                  fontSize: 12.0,
                  color: Color(0xFFFFCC33),
                  decorationColor: Color(0xFFFFCC33),
                  decoration: TextDecoration.underline,
                  decorationStyle: TextDecorationStyle.solid,
                ),
              ),
            ),
            Expanded(
              child: Container(),
            ),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  _other("assets/login/login_icon_weixin.png", () {}),
                  _other("assets/login/login_icon_qq.png", () {}),
                  _other("assets/login/login_icon_weibo.png", () {}),
                ],
              ),
            ),
            SizedBox(
              height: 30.0,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text("登录即代表同意",
                    style: TextStyle(fontSize: 12.0, color: Color(0xFFB4BEC6))),
                GestureDetector(
                  onTap: () {},
                  child: Text(
                    "萌宠管家用户协议、隐私政策",
                    style: TextStyle(
                        fontSize: 12.0,
                        color: Color(0xFFFFCC33),
                        decoration: TextDecoration.underline),
                  ),
                )
              ],
            ),
            SizedBox(
              height: 50.0,
            )
          ],
        ),
      ),
    );
  }

  void eyeOpen() {
    setState(() {
      if (passwordShow == true) {
        passwordShow = false;
      } else {
        passwordShow = true;
      }
    });
  }

  void onTextClear() {
    passController.clear();
    setState(() {
      deleteShow = false;
      submitB = false;
    });
  }
}
